import request from '@/utils/request'
import { setTokenInfo } from '@/utils/storage'

// 获取验证码
export const sendCode = (mobile) => {
  return async () => {
    await request({
      url: `/sms/codes/${mobile}`,
      method: 'GET',
    })
    // console.log(res)
  }
}

//保存token
export const saveToken = (payload) => {
  return {
    type: 'LOGIN/TOKEN',
    payload,
  }
}

/**
 * 登录
 * @param {*} data
 * @returns
 */
export const login = (data) => {
  return async (dispatch) => {
    const res = await request({
      url: '/authorizations',
      method: 'POST',
      data,
    })
    // console.log(res)
    dispatch(saveToken(res.data))
    // 保存token到本地
    setTokenInfo(res.data)
  }
}
